package com.wandian.news.mvvm.knowledge.wordtool;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.google.gson.JsonObject;
import com.mylib.libcore.data.TTBaseAppApi;
import com.mylib.libcore.mvvm.BaseViewModel;
import com.mylib.libcore.network.rx.RxResultHelper;
import com.mylib.libcore.utils.JsonUtil;
import com.mylib.libcore.utils.RetrofitUtil;
import com.mylib.libcore.utils.UIUtils;
import com.wandian.news.bean.KnowledgeCYDGBean;
import com.wandian.news.bean.KnowledgeJFYCBean;
import com.wandian.news.bean.KnowledgeJFZHBean;
import com.wandian.news.bean.KnowledgeMYJJBean;
import com.wandian.news.bean.KnowledgeXHZDBean;
import com.wandian.news.bean.KnowledgeYHZDBean;

import java.util.HashMap;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

import static com.mylib.libcore.config.AppConfig.TOUTIAO_KEY;
import static com.mylib.libcore.config.NewsApiConfig.NEWS_KNOWLEDGE_CYDG;
import static com.mylib.libcore.config.NewsApiConfig.NEWS_KNOWLEDGE_JFYC;
import static com.mylib.libcore.config.NewsApiConfig.NEWS_KNOWLEDGE_JFZH;
import static com.mylib.libcore.config.NewsApiConfig.NEWS_KNOWLEDGE_MYJJ;
import static com.mylib.libcore.config.NewsApiConfig.NEWS_KNOWLEDGE_XHZD;
import static com.mylib.libcore.config.NewsApiConfig.NEWS_KNOWLEDGE_YHZD;
import static com.mylib.libcore.config.NewsApiConfig.NEWS_KNOWLEDGE_ZWCD;


/**
 * @author Administrator
 * @date 2019/12/18-16:11
 * @description KnowledgeWordToolViewModel
 **/
public class KnowledgeWordToolViewModel extends BaseViewModel{
    private TTBaseAppApi baseAppApi;
    public MutableLiveData<KnowledgeJFYCBean.NewslistEntity> dataJFYCBean =  new MutableLiveData<>();
    public MutableLiveData<KnowledgeJFZHBean.NewslistEntity> dataJFZHBean =  new MutableLiveData<>();
    public MutableLiveData<KnowledgeYHZDBean.NewslistEntity> dataYHZDBean =  new MutableLiveData<>();
    public MutableLiveData<List<KnowledgeXHZDBean.NewslistEntity>> dataXHZDBean =  new MutableLiveData<>();
    public MutableLiveData<List<KnowledgeCYDGBean.NewslistEntity>> dataCYDGBean =  new MutableLiveData<>();
    public MutableLiveData<List<KnowledgeMYJJBean.NewslistEntity>> dataMYJJBean =  new MutableLiveData<>();
    public KnowledgeWordToolViewModel(@NonNull Application application) {
        super(application);
        baseAppApi =  RetrofitUtil.getTouTiaoRetrofit().create(TTBaseAppApi.class);
    }

    //近反义词
    public void getJFYCData(String word){
        showDialog();
        JsonObject jsonObject = new JsonObject();
            HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("key",TOUTIAO_KEY);
        hashMap.put("word",word);
        RxResultHelper.getHttpObservableNothing(getLifecycleProvider(), baseAppApi.getTouTiaoList(NEWS_KNOWLEDGE_JFYC,hashMap,jsonObject))
                .subscribe(new Observer<Object>() {

                    @Override
                    public void onSubscribe(Disposable d) {
                    }

                    @Override
                    public void onNext(Object o) {
                        if(!UIUtils.isEmpty(o)){
                            KnowledgeJFYCBean bean = JsonUtil.parseJsonToBean(JsonUtil.toJson(o),KnowledgeJFYCBean.class);
                            if(!UIUtils.isEmpty(bean) && bean.getCode()==200 && !UIUtils.isEmpty(bean.getNewslist()) && bean.getNewslist().size()>0){
                                dataJFYCBean.setValue(bean.getNewslist().get(0));
                            }
                        }
                        dismissDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        dismissDialog();
                    }

                    @Override
                    public void onComplete() {
                    }
                });

    }

    //英汉字典
    public void getYHZDData(String word){
        showDialog();
        JsonObject jsonObject = new JsonObject();
            HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("key",TOUTIAO_KEY);
        hashMap.put("word",word);
        RxResultHelper.getHttpObservableNothing(getLifecycleProvider(), baseAppApi.getTouTiaoList(NEWS_KNOWLEDGE_YHZD,hashMap,jsonObject))
                .subscribe(new Observer<Object>() {

                    @Override
                    public void onSubscribe(Disposable d) {
                    }

                    @Override
                    public void onNext(Object o) {
                        if(!UIUtils.isEmpty(o)){
                            KnowledgeYHZDBean bean = JsonUtil.parseJsonToBean(JsonUtil.toJson(o),KnowledgeYHZDBean.class);
                            if(!UIUtils.isEmpty(bean) && bean.getCode()==200 && !UIUtils.isEmpty(bean.getNewslist()) && bean.getNewslist().size()>0){
                                dataYHZDBean.setValue(bean.getNewslist().get(0));
                            }
                        }
                        dismissDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        dismissDialog();
                    }

                    @Override
                    public void onComplete() {
                    }
                });

    }

    //中文词典
    public void getZWCDData(String word){
        showDialog();
        JsonObject jsonObject = new JsonObject();
            HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("key",TOUTIAO_KEY);
        hashMap.put("word",word);
        RxResultHelper.getHttpObservableNothing(getLifecycleProvider(), baseAppApi.getTouTiaoList(NEWS_KNOWLEDGE_ZWCD,hashMap,jsonObject))
                .subscribe(new Observer<Object>() {

                    @Override
                    public void onSubscribe(Disposable d) {
                    }

                    @Override
                    public void onNext(Object o) {
                        if(!UIUtils.isEmpty(o)){
                            KnowledgeYHZDBean bean = JsonUtil.parseJsonToBean(JsonUtil.toJson(o),KnowledgeYHZDBean.class);
                            if(!UIUtils.isEmpty(bean) && bean.getCode()==200 && !UIUtils.isEmpty(bean.getNewslist()) && bean.getNewslist().size()>0){
                                dataYHZDBean.setValue(bean.getNewslist().get(0));
                            }
                        }
                        dismissDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        dismissDialog();
                    }

                    @Override
                    public void onComplete() {
                    }
                });

    }

    //简繁转换
    public void getJFZHData(String word,String type){
        showDialog();
        JsonObject jsonObject = new JsonObject();
            HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("key",TOUTIAO_KEY);
        hashMap.put("text",word);
        hashMap.put("type",type);
        RxResultHelper.getHttpObservableNothing(getLifecycleProvider(), baseAppApi.getTouTiaoList(NEWS_KNOWLEDGE_JFZH,hashMap,jsonObject))
                .subscribe(new Observer<Object>() {

                    @Override
                    public void onSubscribe(Disposable d) {
                    }

                    @Override
                    public void onNext(Object o) {
                        if(!UIUtils.isEmpty(o)){
                            KnowledgeJFZHBean bean = JsonUtil.parseJsonToBean(JsonUtil.toJson(o),KnowledgeJFZHBean.class);
                            if(!UIUtils.isEmpty(bean) && bean.getCode()==200 && !UIUtils.isEmpty(bean.getNewslist()) && bean.getNewslist().size()>0){
                                dataJFZHBean.setValue(bean.getNewslist().get(0));
                            }
                        }
                        dismissDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        dismissDialog();
                    }

                    @Override
                    public void onComplete() {
                    }
                });

    }

    //新华字典
    public void getXHZDData(String word){
        showDialog();
        JsonObject jsonObject = new JsonObject();
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("key",TOUTIAO_KEY);
        hashMap.put("word",word);
        hashMap.put("num","10");
        hashMap.put("page","1");
        RxResultHelper.getHttpObservableNothing(getLifecycleProvider(), baseAppApi.getTouTiaoList(NEWS_KNOWLEDGE_XHZD,hashMap,jsonObject))
                .subscribe(new Observer<Object>() {

                    @Override
                    public void onSubscribe(Disposable d) {
                    }

                    @Override
                    public void onNext(Object o) {
                        if(!UIUtils.isEmpty(o)){
                            KnowledgeXHZDBean bean = JsonUtil.parseJsonToBean(JsonUtil.toJson(o),KnowledgeXHZDBean.class);
                            if(!UIUtils.isEmpty(bean) && bean.getCode()==200 && !UIUtils.isEmpty(bean.getNewslist()) && bean.getNewslist().size()>0){
                                dataXHZDBean.setValue(bean.getNewslist());
                            }
                        }
                        dismissDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        dismissDialog();
                    }

                    @Override
                    public void onComplete() {
                    }
                });

    }

    //成语典故
    public void getCYDGData(String word){
        showDialog();
        JsonObject jsonObject = new JsonObject();
            HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("key",TOUTIAO_KEY);
        hashMap.put("word",word);
        hashMap.put("num","50");
        hashMap.put("mode","1");
        RxResultHelper.getHttpObservableNothing(getLifecycleProvider(), baseAppApi.getTouTiaoList(NEWS_KNOWLEDGE_CYDG,hashMap,jsonObject))
                .subscribe(new Observer<Object>() {

                    @Override
                    public void onSubscribe(Disposable d) {
                    }

                    @Override
                    public void onNext(Object o) {
                        if(!UIUtils.isEmpty(o)){
                            KnowledgeCYDGBean bean = JsonUtil.parseJsonToBean(JsonUtil.toJson(o),KnowledgeCYDGBean.class);
                            if(!UIUtils.isEmpty(bean) && bean.getCode()==200 && !UIUtils.isEmpty(bean.getNewslist()) && bean.getNewslist().size()>0){
                                dataCYDGBean.setValue(bean.getNewslist());
                            }
                        }
                        dismissDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        dismissDialog();
                    }

                    @Override
                    public void onComplete() {
                    }
                });

    }

    //名言警句
    public void getMYJJData(){
        showDialog();
        JsonObject jsonObject = new JsonObject();
            HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("key",TOUTIAO_KEY);
        hashMap.put("num","10");
        RxResultHelper.getHttpObservableNothing(getLifecycleProvider(), baseAppApi.getTouTiaoList(NEWS_KNOWLEDGE_MYJJ,hashMap,jsonObject))
                .subscribe(new Observer<Object>() {

                    @Override
                    public void onSubscribe(Disposable d) {
                    }

                    @Override
                    public void onNext(Object o) {
                        if(!UIUtils.isEmpty(o)){
                            KnowledgeMYJJBean bean = JsonUtil.parseJsonToBean(JsonUtil.toJson(o),KnowledgeMYJJBean.class);
                            if(!UIUtils.isEmpty(bean) && bean.getCode()==200 && !UIUtils.isEmpty(bean.getNewslist()) && bean.getNewslist().size()>0){
                                dataMYJJBean.setValue(bean.getNewslist());
                            }
                        }
                        dismissDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        dismissDialog();
                    }

                    @Override
                    public void onComplete() {
                    }
                });

    }
}
